package com.wzh.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.wzh.common.exception.BizCodeEnume;
import com.wzh.mall.member.feign.CouponFeignService;
import com.wzh.mall.member.vo.MemberLoginVo;
import com.wzh.mall.member.vo.MemberRegistVo;
import com.wzh.mall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wzh.mall.member.entity.MemberEntity;
import com.wzh.mall.member.service.MemberService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;



/**
 * 会员
 *
 * @author wzh
 * @email wzh3147255630@163.com
 * @date 2021-08-27 10:31:31
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;


    /**
      * @Description: 社交登陆
      * @Param: [vo]
      * @return: com.wzh.common.utils.R
      * @Author: Mr.Wang
      * @Date: 2021/10/8
      * @Time: 16:15
      */
    @PostMapping("/oauth2/login")
    public R oauthlogin(@RequestBody SocialUser socialUser) throws Exception
   {
        MemberEntity memberEntity=memberService.login(socialUser);
        if (memberEntity!=null){
            return R.ok().setDat(memberEntity);
        }else {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }


    }
    
    /**
      * @Description: 会员的注册
      * @Param: []
      * @return: com.wzh.common.utils.R
      * @Author: Mr.Wang
      * @Date: 2021/9/16
      * @Time: 11:22
      */      
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){
        try {
            memberService.regist(vo);
        }catch (Exception e){
            e.printStackTrace();

        }
        return R.ok();
    }

    @RequestMapping("/coupons")
    public R testFeign(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");

        R memberCoupon = couponFeignService.memberCoupon();
        Object coupons = memberCoupon.get("coupons");

        return R.ok().put("member",memberEntity).put("coupons",coupons);
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity memberEntity=memberService.login(vo);
        if (memberEntity!=null){
            return R.ok().setDat(memberEntity);
        }else {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }


    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
